package ThreadTest;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 李泽伟
 */
public class Main {
    public static void main(String[] args) {
        ThreadTest tt = new ThreadTest();
        Thread t1 = new Thread(tt);
        Thread t2 = new Thread(tt);
        t1.setName("账户1");
        t2.setName("账户2");
        t1.start();
        t2.start();
    }
}

class ThreadTest implements Runnable{
    private int saveNum = 100;
    private int leaveMoney = 0;
    private ReentrantLock lock = new ReentrantLock();
    @Override
    public void run() {
        while (true){
            try {
                lock.lock();
                if (saveNum > 0) {
                    leaveMoney += 1000;
                    saveNum -= 1;
                    System.out.println(Thread.currentThread().getName()+"向账户中存储了1000元，余额为："+leaveMoney);
                }else {
                    break;
                }
            }finally {
                lock.unlock();
            }

        }
    }
}